﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Platform.Model;
using Ebada.UI.Base;
using Ebada.Client;
using Ebada.Core;
using Ebada.Client.Platform;
namespace Ebada.Modules.Vehicle
{
    /// <summary>
    /// 违章记录编辑
    /// </summary>
    public partial class frmVehicleActivityBXEdit : FormBase, IPopupFormEdit {

        int day = 0;

        public frmVehicleActivityBXEdit() {
            InitializeComponent();
        }
        void dataBind() {

            this.txtVehicleNumber.DataBindings.Add("EditValue", rowData, "VehicleNumber");
            this.txtAct_Code.DataBindings.Add("EditValue", rowData, "Act_Code");
            this.txtAct_Name.DataBindings.Add("EditValue", rowData, "Act_Name");
            this.txtAccountCode.DataBindings.Add("EditValue", rowData, "AccountCode");
            this.txtAccountTitle.DataBindings.Add("EditValue", rowData, "AccountTitle");

            this.dateDate3.DataBindings.Add("EditValue", rowData, "Date3");
            this.txtCol1.DataBindings.Add("EditValue", rowData, "Col1");
            this.txtCol2.DataBindings.Add("EditValue", rowData, "Col2");
            this.txtBOrgcnName.DataBindings.Add("EditValue", rowData, "BOrgcnName");
            this.txtTotalPrice.DataBindings.Add("EditValue", rowData, "TotalPrice");
            this.txtOfferAmount.DataBindings.Add("EditValue", rowData, "OfferAmount");
            this.dateStartDate.DataBindings.Add("EditValue", rowData, "StartDate");
            this.dateExpirationDate.DataBindings.Add("EditValue", rowData, "ExpirationDate");
            this.txtEmployeeName.DataBindings.Add("EditValue", rowData, "EmployeeName");
            this.txtRemark.DataBindings.Add("EditValue", rowData, "Remark");
        }
        #region IPopupFormEdit Members
        private VehicleActivity rowData = null;

        public object RowData {
            get {
                return rowData;
            }
            set {
                if (value == null) return;
                if (rowData == null) {
                    this.rowData = value as VehicleActivity;
                    dataBind();
                    InitComboBoxData();
                } else {
                    ConvertHelper.CopyTo<VehicleActivity>(value as VehicleActivity, rowData);
                    InitComboBoxData();
                }
            }
        }

        #endregion

        private void InitComboBoxData() {
            IList<Activities> list = MainHelper.GetSqlMap<Activities>().GetListByWhere<Activities>(" where ProjectName='车辆项目' and Act_Name='保险'");
            if (list != null && list.Count > 0)
            {
                rowData.Act_Code = list[0].Act_Code;
                rowData.Act_Name = list[0].Act_Name;
            }
            IList<Vehicles> vlist = MainHelper.GetSqlMap<Vehicles>().GetListByWhere<Vehicles>(" order by VehicleNumber");
            txtVehicleNumber.Properties.DataSource = vlist;
            IList<Accounts> acclist = MainHelper.GetSqlMap<Accounts>().GetListByWhere<Accounts>(" where DebitOrCredit like '%行政%' order by AccountCode");
            txtAccountTitle.Properties.DataSource = acclist;

            if (rowData.VehicleNumber == "")
            {
                rowData.Date3 = DateTime.Now;
                rowData.StartDate = DateTime.Now;
                rowData.ExpirationDate = DateTime.Now;
            }

            IList<VehicleWarningType> v = MainHelper.GetSqlMap<VehicleWarningType>().GetListByWhere<VehicleWarningType>(" where Act_Name='保险'");
            if (v.Count>0)
            {
                day = v[0].Days;
            }
        }
 
        private void btnOK_Click(object sender, EventArgs e)
        {
            
            if (txtAccountTitle.Text == "")
            {
                MessageBox.Show("科目名称不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtAccountTitle.Focus();
                return;
            }
            if (txtVehicleNumber.Text == "")
            {
                MessageBox.Show("车牌号不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtVehicleNumber.Focus();
                return;
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

       
        private void txtAccountTitle_EditValueChanged(object sender, EventArgs e)
        {
            if (txtAccountTitle.Text != "")
            {
                rowData.AccountCode = ((Accounts)txtAccountTitle.Properties.GetDataSourceRowByKeyValue(txtAccountTitle.Text)).AccountCode;
                txtAccountCode.Text = rowData.AccountCode;
            }
        }

        private void txtVehicleNumber_EditValueChanged(object sender, EventArgs e)
        {
            if (txtVehicleNumber.Text != "")
            {
                rowData.Col1 = ((Vehicles)txtVehicleNumber.Properties.GetDataSourceRowByKeyValue(txtVehicleNumber.Text)).VehicleType;
                txtCol1.Text = rowData.Col1;

            }
        }

        private void dateExpirationDate_EditValueChanged(object sender, EventArgs e)
        {
            rowData.Date3 = dateExpirationDate.DateTime.AddDays(-day);
            dateDate3.DateTime = rowData.Date3;
        }
      
    }
}